#ifndef PORTOBJECT_H
#define PORTOBJECT_H


#include <QObject>
#include <QThread>
#include <QSerialPort>
#include <QMutex>
#include <QWaitCondition>
#include <QTimer>


class BeidouObject : public QObject
{
    Q_OBJECT
public:
    explicit BeidouObject(QObject *parent = nullptr);

    explicit BeidouObject(QString COMname);


    ~BeidouObject();

    QSerialPort *myPort;

    QTimer *timerReadAll;
    QString revBuffer;

    /**
     * @brief 初始化串口
     * @param COMname
     */
    void init_port(QString COMname);  //




    QMutex mutex;
    QWaitCondition waitCon;
    QString COMName;


    /**
     * @brief initialize port and start Thread of port
     * @param COMName
     */
    void initialize_port_start_thread(QString COMName);
    void analyseData(QString revBuffer);
    void wakeup_mutex();

    /*
     * 线程是否结束
     */
    volatile  bool isTerminate = false;

    bool func_stop(unsigned long mytime);
signals:

    //接收数据
    void receive_data(QByteArray tmp);
    //像界面发送解析后的数据
    void transfer_result(QStringList result);
    // 发送数据
    void send_data(QString sendData);
    void signal_stop();

public slots:

    void handle_data();  //处理接收到的数据
    void write_data(QString send_data);     //发送数据
    void slot_stop( );     //
    // 定时器超时读取串口缓冲区数据
    void timeUpdateReadAll();



private:
    QThread *my_thread;

};

#endif // PORTOBJECT_H
